package at.fhooe.mc.android.shiftalarmclock;

import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ToggleButton;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {

	private static final String LOG_TAG = "MainActivity";
	// List with entries
	public static ArrayList<DummyData> list = new ArrayList<DummyData>();
	public static ArrayList<DummyDataMain> listMain = new ArrayList<DummyDataMain>();
	public static int pointer = 0; // to entry

	// put pointer to next element

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		Button b = (Button) (findViewById(R.id.button1));
		b.setOnClickListener(this);
		ToggleButton tbOn_Off = (ToggleButton) (findViewById(R.id.toggleButton1));
		tbOn_Off.setOnClickListener(this);
	}

	@SuppressLint("ShowToast")
	public void onClick(View _v) {
		switch (_v.getId()) {
		case R.id.button1: {
			Intent i = new Intent(this, Settings.class);
			startActivity(i);
			Log.i("AlarmClock", "Button settings pressed");
		}
			break;
		case R.id.toggleButton1: {
			// set Alarm On/Off and give ah Toast message
			if (AlarmModel.AlarmOnOff == true) {
				AlarmModel.AlarmOnOff = false;
				Toast toast = Toast.makeText(getBaseContext(),
						R.string.text_alarm_on, 2000);
				toast.show();
				Log.i("AlarmClock", "ToggleButton On pressed");
			} else {
				AlarmModel.AlarmOnOff = true;
				Toast toast = Toast.makeText(getBaseContext(),
						R.string.text_alarm_off, 2000);
				toast.show();
				Log.i("AlarmClock", "ToggleButton Off pressed");
			}
		}
			break;
		}
	}

	public void onResume() {
		super.onResume();

		Log.i(LOG_TAG, "in onResume()");
		if (listMain.size() >= 0) {
			@SuppressWarnings("rawtypes")
			ArrayAdapter adapter = new MyArrayAdapter(this,
					android.R.layout.activity_list_item, listMain);
			ListView lv = (ListView) findViewById(R.id.listViewMain);
			lv.setAdapter(adapter);
		}

		// Status Bar
		Intent i = new Intent(this, MainActivity.class);
		PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

		Notification.Builder builder = new Notification.Builder(this);
		builder.setSmallIcon(R.drawable.ic_launcher);

		if (listMain.size() != 0) {
			String time = list.get(pointer).getAlarmTime();
			builder.setContentTitle("Next Alarm: " + time);
		} else {
			String s = getResources().getString(R.string.text_no_alarm);
			builder.setContentTitle(String.valueOf(s));
		}
		builder.setAutoCancel(true);
		builder.setContentIntent(pi);

		Notification noti = builder.build();

		NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		nMgr.notify(666, noti);
		// end Status Bar

	}

	public void triggerAlarm() {
		int d = MainActivity.pointer;
		Calendar cal = Calendar.getInstance();
		Calendar calSet = (Calendar) cal.clone();
		DummyDataMain a = MainActivity.listMain.get(MainActivity.pointer);

		if (AlarmModel.days[d] == true) {
			Intent activate = new Intent(getBaseContext(), AlarmReceiver.class);
			AlarmManager alarms;
			PendingIntent alarmIntent = PendingIntent.getBroadcast(
					getBaseContext(), 0, activate, 0);
			alarms = (AlarmManager) getBaseContext().getSystemService(
					Context.ALARM_SERVICE);

			AlarmModel aModel = new AlarmModel();
			calSet.setTimeInMillis(aModel.getTime(a));
			alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
					alarmIntent);
		}
		pointer = (pointer + 1) % listMain.size();
	}
}